<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>MybatisLog</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
</head>

<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend style="margin-left:2px">
            <span class="layui-badge-dot"></span>
            Mybatis日志转换
        </legend>
    </fieldset>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label"><a style="font-size: 20px">日志内容</a></label>
        <div class="layui-input-block">
            <textarea style="height: 300px;" placeholder="在此处粘贴日志内容" id="log" name="log"
                class="layui-textarea"></textarea>
        </div>
    </div>
    <div style="margin-left: 110px;">
        <button type="button" class="layui-btn layui-btn-warm" onclick="transLog()">转换并复制</button>
        <button type="button" class="layui-btn layui-btn-warm" onclick="clearLog()">清空输入 </button>
        <button type="button" class="layui-btn layui-btn-warm" onclick="clearOut()">清空输出 </button>
    </div>
    <br>
    <br>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">
            <a style="font-size: 20px">
                SQL<br>语句
            </a>
        </label>
        <div class="layui-input-block">
            <textarea style="border: 2px solid red; display: none; height: 300px;" id="sql" name="sql" readonly
                class="layui-textarea"></textarea>
        </div>
    </div>
    <script src="layui/layui.js" charset="utf-8"></script>
    <script>
        /**
         * 处理日志
         */
        function transLog() {
            var textVa = document.getElementById("log").value;
            getSql(textVa);
            copyToClip();
        }

        /**
         * 解析sql
         */
        function getSql(textVa) {
            // 获取带问号的SQL语句
            var statementStartIndex = textVa.indexOf('Preparing: ');
            var statementEndIndex = textVa.length - 1;
            for (var i = statementStartIndex; i < textVa.length; i++) {
                if (textVa[i] == "\n") {
                    statementEndIndex = i;
                    break;
                }
            }
            var statementStr = textVa.substring(statementStartIndex + "Preparing: ".length, statementEndIndex);
            statementStr = statementStr.toUpperCase();
            // console.log(statementStr);
            //获取参数
            var parametersStartIndex = textVa.indexOf('Parameters: ');
            var parametersEndIndex = textVa.length;
            for (var i = parametersStartIndex; i < textVa.length; i++) {
                if (textVa[i] == "\n") {
                    parametersEndIndex = i;
                    break;
                } /*else {
                    // console.log(textVa[i]);
                }*/
            }
            var parametersStr = textVa.substring(parametersStartIndex + "Parameters: ".length, parametersEndIndex);
            // console.log(parametersStr);
            // 参数列表
            var parametersStrArr = parametersStr.split(",");
            for (var i = 0; i < parametersStrArr.length; i++) {
                tempStr = parametersStrArr[i].substring(0, parametersStrArr[i].indexOf("("));
                // 不含"("是null
                if (tempStr == '') {
                    tempStr = "null";
                }
                // 如果数据中带括号需要判断参数类型
                typeStr = parametersStrArr[i].substring(parametersStrArr[i].indexOf("(") + 1, parametersStrArr[i].indexOf(")"));
                if (typeStr == "String" || typeStr == "Timestamp") {
                    statementStr = statementStr.replace("?", "'" + tempStr.trim() + "'");
                } else {
                    statementStr = statementStr.replace("?", tempStr.trim());
                }
            }
            document.getElementById("sql").style.display = "inline-block";
            document.getElementById("sql").innerHTML += statementStr;
            var newText = textVa.substring(parametersEndIndex);
            // console.log(newText);
            // console.log(newText.indexOf('Preparing: '));
            if (newText.indexOf('Preparing: ') == -1) return;
            var splitLine = ";\n-- ---------------------------------------------------------------------------------------------------------------------\n";
            document.getElementById("sql").innerHTML += splitLine;
            getSql(newText);
        }

        /**
         * 复制到剪贴板
         */
        function copyToClip() {
            var sql = document.getElementById("sql");
            sql.select();  // 选择对象
            document.execCommand("copy"); // 执行浏览器复制命令
            if ("" != sql.value) {
                layer.msg("复制成功，直接粘贴即可");
            } else {
                layer.alert("转换失败");
            }
        }

        /**
         * 清空输入
         */
        function clearLog() {
            var log = document.getElementById("log");
            log.select();
            log.value = "";
        }

        /**
         * 清空输出
         */
        function clearOut() {
            var sql = document.getElementById("sql");
            sql.innerHTML = "";
        }

    </script>
</body>

</html>
